package com.specialized.pay.controller;

import com.alipay.api.AlipayApiException;
import com.specialized.common.core.domain.Result;
import com.specialized.common.core.utils.uuid.UUID;
import com.specialized.common.redis.service.RedisService;
import com.specialized.pay.service.PayService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.ApplicationContext;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.concurrent.TimeUnit;

/**
 * @Author：wangxinyu
 * @Package：com.specialized.pay.controller
 * @Project：Specialized-Cloud
 * @name：PayController
 * @Date：2024/7/19 18:49
 */
@RestController
@RequestMapping("pay")
public class PayController {
    @Autowired
    private PayService payService;
    @Autowired
    private RedisService redisService;
    @Resource
    private ApplicationContext applicationContext;

    /**
     * 下单支付接口
     * @param orderCard
     * @return,
     */
    @GetMapping("alipayTradePagePay")
    public Result alipayTradePagePay (@RequestParam String orderCard,@RequestParam String payType) throws AlipayApiException {
        PayService bean = (PayService) applicationContext.getBean(payType);
        String payResult = bean.alipayTradePagePay(orderCard);
        return Result.success(payResult);
    }

    /**
     * 交易退款
     * @param tradeNo
     * @throws AlipayApiException
     */
    @PostMapping("alipayTradeRefund")
    public Result alipayTradeRefund (@RequestParam String tradeNo) throws AlipayApiException {
        payService.alipayTradeRefund(tradeNo);
        return Result.success();
    }


    /**
     * 交易关闭
     * @param tradeNo 三方交易流水号
     * @throws AlipayApiException
     */
    @PostMapping("/closeAlipayTradePagePay")
    public Result closeAlipayTradePagePay(String tradeNo) throws AlipayApiException{
        payService.closeAlipayTradePagePay(tradeNo);
        return Result.success();
    }

    /**
     * 异步回调
     * @param request
     * @return
     * @throws AlipayApiException
     */
    @PostMapping("notify_url")
    public Result notifyUrl (HttpServletRequest request) throws AlipayApiException {
        PayService bean = (PayService) applicationContext.getBean("zfb");
        String payResult = bean.notifyUrl(request);
        return Result.success(payResult);
    }



    /**
     * 交易查询接口
     * @param TradeNo
     * @throws AlipayApiException
     */
    @PostMapping("/alipayTradeQuery")
    public Result alipayTradeQuery( String TradeNo) throws AlipayApiException {
        String status = payService.alipayTradeQuery(TradeNo);
        return Result.success(status);
    }




}
